package com.xu.tasks;

import com.xu.pojo.MovieDetail;
import com.xu.service.MovieDetailService;
import com.xu.uitls.UserAgentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

@Component
public class MovieDetailPageProcessor implements PageProcessor {
    private String url;
    private MovieDetail movieDetail;
    @Autowired
    private MovieDetailService movieDetailService;

    public MovieDetail getMovieDetail() {
        return movieDetail;
    }

    public void setMovieDetail(MovieDetail movieDetail) {
        this.movieDetail = movieDetail;
    }

    private Site site = Site.me()
            .setCharset("utf-8") //设置编码
            .addHeader("Cookie","_lxsdk_cuid=176571e40e6c8-011fb5ea0a6c44-5a301e42-1fa400-176571e40e6c8; uuid_n_v=v1; uuid=F423B210AFBA11EBB66D1FD9F82B073EAC7995061B924E30BA1477686E3488DC; _csrf=bdc239883691a6ca59c96b77025346c94faa24ba0b3a7bc913ee64bfb095d038; lt=i41AXO94hbY94wHs-vu5ldo5aHEAAAAAhw0AAK2pUUULXo3La4S3GxU2a8Z5e9Go6BGMN3eAs2B2-puiQFabpVMD4IVMYu__mzeWIg; lt.sig=ggxdEM-ZCxHaiNrG0DgDfpD-ZwI; uid=929748733; uid.sig=BqUXWSSkF3Q4E3siclw8YrZLihg; _lxsdk=F423B210AFBA11EBB66D1FD9F82B073EAC7995061B924E30BA1477686E3488DC; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1620450016,1620450308,1620450313,1620463295; __mta=48520601.1607779369251.1620463606834.1620463607778.134; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1620463685; _lxsdk_s=1794b0da894-164-e62-85a%7C929748733%7C138")
            .setCycleRetryTimes(3000) //设置重试时间间隔
            .setTimeOut(10000)//超时时间
            .setSleepTime(3); //设置重试次数

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public void process(Page page) {
        List<Selectable> types = page.getHtml().css("li.ellipsis a").nodes();

        String type = new String();
        String time = new String();
        String area = new String();
        String showTime = new String();
        String introduce = new String();
        List<Selectable> nodes = page.getHtml().css("li.ellipsis").nodes();
        type = nodes.get(0).css("a.text-link", "text").all().toString();
        type = type.substring(1,type.length() - 1).trim();
        movieDetail.setType(type);

        List<Selectable> nodes1 = page.getHtml().css("li.ellipsis").nodes();
        String tmp = nodes1.get(1).css("li","text").toString();
        area = tmp.substring(0,tmp.indexOf("/"));
        time = tmp.substring(tmp.indexOf("/") + 1);
        movieDetail.setArea(area);
        movieDetail.setTime(time);

        showTime = nodes1.get(2).css("li","text").toString();
        movieDetail.setShowTime(showTime);

        introduce = page.getHtml().css("span.dra", "text").toString();

        movieDetail.setTime(time);
        movieDetail.setArea(area);
        movieDetail.setType(type);
        movieDetail.setShowTime(showTime);
        movieDetail.setIntroduce(introduce);

        System.out.println(movieDetail);
    }

    public MovieDetail exec(){
        site.setUserAgent(UserAgentUtil.getUserAgent());
        Spider.create(this)
                .addUrl(this.getUrl())
                .thread(5)
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(1000)))
                .run();
        return movieDetail;
    }

    @Override
    public Site getSite() {
        return site;
    }
}
